$(function() {
	$('.nav').superfish({
		hoverClass	 : 'sfHover',
		pathClass	 : 'overideThisToUse',
		delay		 : 0,
		animation	 : {height: 'show'},
		speed		 : 'normal',
		autoArrows   : false,
		dropShadows  : false, 
		disableHI	 : false, /* set to true to disable hoverIntent detection */
		onInit		 : function(){},
		onBeforeShow : function(){},
		onShow		 : function(){},
		onHide		 : function(){}
	});
	
	$('.nav').css('display', 'block');
	$( "#tabs" ).tabs();
	$("button, .button, input[type='button']").button();
	$( ".filter" ).button({icons: {secondary: "ui-icon-shuffle"}, text: false});
	$( ".status" ).button({icons: {primary: "ui-icon-lightbulb"}, text: false});
	$( ".edit" ).button({icons: {primary: "ui-icon-wrench"}, text: false});
	$( ".delete" ).button({icons: {primary: "ui-icon-trash"}, text: false});
	
	$('.ui-state-default').hover(
		function(){ $(this).addClass('ui-state-hover'); }, 
		function(){ $(this).removeClass('ui-state-hover'); }
	);

	$.postJSON = function(url, data, callback) {
		$.post(url, data, callback, "json");
	};
	
	$('.tablesorter tbody tr:odd > td, .tableselector tbody tr:odd > td').addClass('row_odd');
	$('.tableselector tbody tr').hover(
		function(){ $(this).children('td').addClass('row_hover'); }, 
		function(){ $(this).children('td').removeClass('row_hover'); }
	).live('click', function() {
		var status = $(this).find('input[type="checkbox"]').attr('checked');
		console.log(status);
		if(status) {
			$(this).find('td').removeClass('row_selected').find('input[type="checkbox"]').attr('checked', false);
		} else {
			$(this).find('td').addClass('row_selected').find('input[type="checkbox"]').attr('checked', true);
		}
	});
	
	$('.tableselector tbody tr:has(input:checked) > td').addClass('row_selected');
});

function split(val) {
	return val.split(/,\s*/);
}

function extractLast(term) {
	return split(term).pop();
}

function str_rand() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 4;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum, rnum + 1);
	}
	return '_' + randomstring;
}

function decode_string(text) {
	var str = $.trim(text);

	str = str.replace(/\u00E1/g, 'a');
	str = str.replace(/\u00C1/g, 'a');
	str = str.replace(/\u00E0/g, 'a');
	str = str.replace(/\u00C0/g, 'a');
	str = str.replace(/\u1EA3/g, 'a');
	str = str.replace(/\u1EA2/g, 'a');
	str = str.replace(/\u00E3/g, 'a');
	str = str.replace(/\u00C3/g, 'a');
	str = str.replace(/\u1EA1/g, 'a');
	str = str.replace(/\u1EA0/g, 'a');
	str = str.replace(/\u0103/g, 'a');
	str = str.replace(/\u0102/g, 'a');
	str = str.replace(/\u1EAF/g, 'a');
	str = str.replace(/\u1EAE/g, 'a');
	str = str.replace(/\u1EB1/g, 'a');
	str = str.replace(/\u1EB0/g, 'a');
	str = str.replace(/\u1EB3/g, 'a');
	str = str.replace(/\u1EB2/g, 'a');
	str = str.replace(/\u1EB5/g, 'a');
	str = str.replace(/\u1EB4/g, 'a');
	str = str.replace(/\u1EB7/g, 'a');
	str = str.replace(/\u1EB6/g, 'a');
	str = str.replace(/\u00E2/g, 'a');
	str = str.replace(/\u00C2/g, 'a');
	str = str.replace(/\u1EA5/g, 'a');
	str = str.replace(/\u1EA4/g, 'a');
	str = str.replace(/\u1EA7/g, 'a');
	str = str.replace(/\u1EA6/g, 'a');
	str = str.replace(/\u1EA9/g, 'a');
	str = str.replace(/\u1EA8/g, 'a');
	str = str.replace(/\u1EAB/g, 'a');
	str = str.replace(/\u1EAA/g, 'a');
	str = str.replace(/\u1EAD/g, 'a');
	str = str.replace(/\u1EAC/g, 'a');
	str = str.replace(/\u00E9/g, 'e');
	str = str.replace(/\u00C9/g, 'e');
	str = str.replace(/\u00E8/g, 'e');
	str = str.replace(/\u00C8/g, 'e');
	str = str.replace(/\u1EBB/g, 'e');
	str = str.replace(/\u1EBA/g, 'e');
	str = str.replace(/\u1EBD/g, 'e');
	str = str.replace(/\u1EBC/g, 'e');
	str = str.replace(/\u1EB9/g, 'e');
	str = str.replace(/\u1EB8/g, 'e');
	str = str.replace(/\u00EA/g, 'e');
	str = str.replace(/\u00CA/g, 'e');
	str = str.replace(/\u1EBF/g, 'e');
	str = str.replace(/\u1EBE/g, 'e');
	str = str.replace(/\u1EC1/g, 'e');
	str = str.replace(/\u1EC0/g, 'e');
	str = str.replace(/\u1EC3/g, 'e');
	str = str.replace(/\u1EC2/g, 'e');
	str = str.replace(/\u1EC5/g, 'e');
	str = str.replace(/\u1EC4/g, 'e');
	str = str.replace(/\u1EC7/g, 'e');
	str = str.replace(/\u1EC6/g, 'e');
	str = str.replace(/\u00ED/g, 'i');
	str = str.replace(/\u00CD/g, 'i');
	str = str.replace(/\u00EC/g, 'i');
	str = str.replace(/\u00CC/g, 'i');
	str = str.replace(/\u1EC9/g, 'i');
	str = str.replace(/\u1EC8/g, 'i');
	str = str.replace(/\u0129/g, 'i');
	str = str.replace(/\u0128/g, 'i');
	str = str.replace(/\u1ECB/g, 'i');
	str = str.replace(/\u1ECA/g, 'i');
	str = str.replace(/\u00F3/g, 'o');
	str = str.replace(/\u00D3/g, 'o');
	str = str.replace(/\u00F2/g, 'o');
	str = str.replace(/\u00D2/g, 'o');
	str = str.replace(/\u1ECF/g, 'o');
	str = str.replace(/\u1ECE/g, 'o');
	str = str.replace(/\u00F5/g, 'o');
	str = str.replace(/\u00D5/g, 'o');
	str = str.replace(/\u1ECD/g, 'o');
	str = str.replace(/\u1ECC/g, 'o');
	str = str.replace(/\u01A1/g, 'o');
	str = str.replace(/\u01A0/g, 'o');
	str = str.replace(/\u1EDB/g, 'o');
	str = str.replace(/\u1EDA/g, 'o');
	str = str.replace(/\u1EDD/g, 'o');
	str = str.replace(/\u1EDC/g, 'o');
	str = str.replace(/\u1EDF/g, 'o');
	str = str.replace(/\u1EDE/g, 'o');
	str = str.replace(/\u1EE1/g, 'o');
	str = str.replace(/\u1EE0/g, 'o');
	str = str.replace(/\u1EE3/g, 'o');
	str = str.replace(/\u1EE2/g, 'o');
	str = str.replace(/\u00F4/g, 'o');
	str = str.replace(/\u00D4/g, 'o');
	str = str.replace(/\u1ED1/g, 'o');
	str = str.replace(/\u1ED0/g, 'o');
	str = str.replace(/\u1ED3/g, 'o');
	str = str.replace(/\u1ED2/g, 'o');
	str = str.replace(/\u1ED5/g, 'o');
	str = str.replace(/\u1ED4/g, 'o');
	str = str.replace(/\u1ED7/g, 'o');
	str = str.replace(/\u1ED6/g, 'o');
	str = str.replace(/\u1ED9/g, 'o');
	str = str.replace(/\u1ED8/g, 'o');
	str = str.replace(/\u00FA/g, 'u');
	str = str.replace(/\u00DA/g, 'u');
	str = str.replace(/\u00F9/g, 'u');
	str = str.replace(/\u00D9/g, 'u');
	str = str.replace(/\u1EE7/g, 'u');
	str = str.replace(/\u1EE6/g, 'u');
	str = str.replace(/\u0169/g, 'u');
	str = str.replace(/\u0168/g, 'u');
	str = str.replace(/\u1EE5/g, 'u');
	str = str.replace(/\u1EE4/g, 'u');
	str = str.replace(/\u01B0/g, 'u');
	str = str.replace(/\u01AF/g, 'u');
	str = str.replace(/\u1EE9/g, 'u');
	str = str.replace(/\u1EE8/g, 'u');
	str = str.replace(/\u1EEB/g, 'u');
	str = str.replace(/\u1EEA/g, 'u');
	str = str.replace(/\u1EED/g, 'u');
	str = str.replace(/\u1EEC/g, 'u');
	str = str.replace(/\u1EEF/g, 'u');
	str = str.replace(/\u1EEE/g, 'u');
	str = str.replace(/\u1EF1/g, 'u');
	str = str.replace(/\u1EF0/g, 'u');
	str = str.replace(/\u00FD/g, 'y');
	str = str.replace(/\u00DD/g, 'y');
	str = str.replace(/\u1EF3/g, 'y');
	str = str.replace(/\u1EF2/g, 'y');
	str = str.replace(/\u1EF7/g, 'y');
	str = str.replace(/\u1EF6/g, 'y');
	str = str.replace(/\u1EF9/g, 'y');
	str = str.replace(/\u1EF8/g, 'y');
	str = str.replace(/\u1EF5/g, 'y');
	str = str.replace(/\u1EF4/g, 'y');
	str = str.replace(/\u0110/g, 'd');
	str = str.replace(/\u0111/g, 'd');
	str = str.replace(/'/g, "");
	str = str.replace(/"/g, "");
	
	str = str.replace(/ /g, "-");
	
	return str;
}

function do_search() {
	var keyword = $.trim($('#keyword').val());
	if (keyword.length < 3) {
		return false;
	}

	query = decode_string(keyword) + ".html";
	window.location.href = sitepath + "results-" + query;
}

function handleEnter(field, event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which
			: event.charCode;
	if (keyCode == 13) {
		do_search();
		return false;
	} else {
		return true;
	}
}

function do_check(fields) {
	$('.'+fields).attr('checked', true)
	.parent('td').addClass('row_selected')
	.next().addClass('row_selected');
}

function do_uncheck(fields) {
	$('.'+fields).attr('checked', false)
	.parent('td').removeClass('row_selected')
	.next().removeClass('row_selected');
}